﻿'===========================================
'GripLabel.vb
' by Ken T Ekeoha
'
'===========================================










Public NotInheritable Class GripLabel : Inherits System.Windows.Controls.Label

    Public PuppetWindow As MoveableWindow
    Public IsActive As Boolean = True
    Dim _puppetWinOffsetX As Double = -1
    Dim _puppetWinOffsetY As Double = -1
    Dim locked As Boolean = False

    Protected Ygap As Double = 0
    Protected Xgap As Double = 8

    'Public Sub on_MMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseMove

    '    If locked And PuppetWindow IsNot Nothing Then
    '        If PuppetWindow.WindowStyle = Windows.WindowStyle.None Then
    '            Ygap = 8
    '        Else
    '            Ygap = 30
    '        End If

    '        PuppetWindow.Left = PointToScreen(e.GetPosition(Me)).X - (Xgap + _handleOffsetX + Margin.Left)
    '        PuppetWindow.Top = PointToScreen(e.GetPosition(Me)).Y - (Ygap + _handleOffsetY + Margin.Top)
    '    End If


    'End Sub
    'Public Sub on_MUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseUp
    '    locked = False
    '    System.Windows.Input.Mouse.Captured.ReleaseMouseCapture()
    '    '  winHandle.ReleaseMouseCapture()
    'End Sub
    'Public Sub on_MDown(ByVal sender As Object, ByVal e As Windows.Input.MouseEventArgs) Handles Me.MouseDown

    '    If e.LeftButton = Windows.Input.MouseButtonState.Pressed Then
    '        If Not locked Then
    '            _handleOffsetX = e.GetPosition(Me).X
    '            _handleOffsetY = e.GetPosition(Me).Y
    '            locked = True
    '        End If
    '        Windows.Input.Mouse.Capture(Me)
    '    End If

    'End Sub


    Dim xy As Point
    Public Sub on_MMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseMove

        'controlla che il mouse sia ancora premuto. Talvolta accade che locked sia erroneamente true nonostante il mouse non sia premuto.
        If PuppetWindow IsNot Nothing And IsActive Then
            If locked And PuppetWindow.IsActive And Mouse.LeftButton = MouseButtonState.Pressed Then
                If PuppetWindow.WindowStyle = Windows.WindowStyle.None Then
                    Ygap = 8
                Else
                    Ygap = 0
                End If

                '   Try
                xy = Me.PointToScreen(e.GetPosition(Me))  'TODO: Esiste un problema location changed viene chiamato dalla prima istruzione e poi dopo chiama la seconda. Dato che location changed chiude PuppetWindow, la seconda viene eseguita con un puppetwindow chiuso ovvero non più connesso alla PresentationSource.
                PuppetWindow.MoveTo(xy.X - (Xgap + _puppetWinOffsetX), xy.Y - (Ygap + _puppetWinOffsetY))
                '     Catch
                '   End Try
            Else
                locked = False
                ReleaseMouseCapture()
            End If
        End If

            MyBase.OnMouseMove(e)
    End Sub
    Public Sub on_MUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseUp
        locked = False
        ReleaseMouseCapture()
        '  winHandle.ReleaseMouseCapture()

    End Sub
    Public Sub on_MDown(ByVal sender As Object, ByVal e As Windows.Input.MouseEventArgs) Handles Me.MouseDown
        If PuppetWindow IsNot Nothing And IsActive Then
            If e.LeftButton = Windows.Input.MouseButtonState.Pressed Then
                If Not locked Then
                    _puppetWinOffsetX = Mouse.GetPosition(PuppetWindow).X
                    _puppetWinOffsetY = Mouse.GetPosition(PuppetWindow).Y
                    locked = True
                Else
                    MessageBox.Show("ERR.ITA. Non dovresti vedermi." & vbCrLf & "ERR.ENG. If you can see this messagebox, there is a problem.")
                End If

                Windows.Input.Mouse.Capture(Me)
            End If
        End If
    End Sub

    Public Sub BreakBind()
        locked = False
        ReleaseMouseCapture()
        PuppetWindow = Nothing
    End Sub
End Class
